home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / printusg.inc < prev    next >
Text File  |  1985-06-03  |  3KB  |  96 lines

  1. {========================== Print_Using ===============================}
  2.  
  3. { This function provides numeric output formatting similar to that found
  4.   in BASIC.  By specifying a mask of the form ($##,###,###.##) you can
  5.   cause commas to be inserted in the number and control the size of the
  6.   print field and the number of decimal places displayed.  If left and
  7.   right parentheses are included in the mask, negative numbers will be
  8.   enclosed in parentheses otherwise they will be preceeded by a minus
  9.   sign.
  10.  
  11.   The device parameter controls whether the output goes to the console (c)
  12.   or the printer (p).  The device parameter may be either upper or lower
  13.   case.
  14.  
  15.   Written by:  Bill Collins.
  16.   Modified by: Bill Todd.
  17.  
  18.   NOTE: The following type declaration will be global to the program that
  19.         uses this function.  Watch out for name conflicts. }
  20.  
  21. type
  22.     print_using_type = string[80];
  23.  
  24.  
  25. Function Print_using(mask: print_using_type; number: real): print_using_type;
  26.  
  27. const
  28.     comma:             char = ',';
  29.     point:             char = '.';
  30.     minus_sign:        char = '-';
  31.     dollar_sign:       char = '$';
  32.     left_paren:        char = '(';
  33.     right_paren:       char = ')';
  34.  
  35. var
  36.     field_width,
  37.     integer_length,
  38.     i,
  39.     j,
  40.     places,
  41.     point_position:     integer;
  42.     using_commas,
  43.     parens,
  44.     dollars,
  45.     decimal,
  46.     negative:           boolean;
  47.     out_string,
  48.     integer_string:     string[80];
  49.  
  50. begin
  51.     negative            := number < 0;
  52.     number              := Abs(number);
  53.     places              := 0;
  54.     field_width         := Length(mask);
  55.     using_commas        := Pos(comma,mask) > 0;
  56.     decimal             := Pos(point,mask) > 0;
  57.     dollars             := Pos(dollar_sign,mask) > 0;
  58.     parens              := Pos(left_paren,mask) > 0;
  59.  
  60.     If decimal then
  61.       begin
  62.           point_position := Pos(point,mask);
  63.  
  64.           If parens then
  65.               places := field_width - 1 - point_position
  66.           else
  67.               places := field_width - point_position;
  68.       end;
  69.  
  70.     Str(number: 0: places, out_string);
  71.  
  72.     If using_commas then
  73.       begin
  74.           j := 0;
  75.           integer_string := Copy(out_string, 1, Length(out_string) - places);
  76.           integer_length := Length(integer_string);
  77.           If decimal then integer_length := integer_length - 1;
  78.  
  79.           For i := integer_length downto 2 do
  80.             begin
  81.                 j := j + 1;
  82.                 If j mod 3 = 0 then Insert(comma, out_string, i);
  83.             end;
  84.       end;  { if using_commas }
  85.  
  86.     If dollars then out_string := dollar_sign + out_string;
  87.  
  88.     If negative then
  89.         If parens then
  90.             out_string := left_paren + out_string + right_paren
  91.         else
  92.             out_string := minus_sign + out_string;
  93.  
  94.     Print_using := out_string;
  95. end;
  96.